﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vmago.Models
{
    public class RepositoryRecShop:IRecShop
    {
        #region IRecShop Members

        private RecShopDataContext ds;

        public RepositoryRecShop()
        {
            ds = new RecShopDataContext(); 
        }

        public IEnumerable<RecShop> getRecShop(int category, int city,int grade)
        { 
             IEnumerable<RecShop> c = null;
          
            try
            {
              c  = from p in ds.RecShops
                   where p.category.Equals(category) && p.city.Equals(city) && p.grade.Equals(grade)
                   select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
            }
            return c;
        }

        public List<RecShop> getRelatedShop(int shopID, int perPage, int page)
        {
            List<RecShop> c = null;

            try
            {
                c = ds.getShopRelatedShop(shopID, perPage, page).ToList();

            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
            }
            return c;
        }


        public IEnumerable<RecShop> getRecShop(int city, int grade)
        {
            IEnumerable<RecShop> c = null;
            try
            {
                c = from p in ds.RecShops
                    where p.city.Equals(city) && p.grade.Equals(grade)
                    select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        #endregion

        #region IRecShop Members


        public List<RecShop> getRecShop( int city,int category, int grade, int perPage, int page)
        {
            List<RecShop> c = null;
            try
            {
                c = ds.getRecShop(city, category, grade, perPage, page).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
               
            }
            return c;
        }

        #endregion

        #region IRecShop Members


        public int getRecShopTotalNumber(int category, int city, int grade)
        {   int r = 0;
            IEnumerable<RecShop> c = null;
            try
            {
                 c=from p in ds.RecShops
                   where p.category.Equals(category) && p.city.Equals(city) && p.grade.Equals(grade)
                   select p;
            
                

                 try 
                 {
                      r =c.Count();
                 }
                 catch ( Exception e)
                 {
                     r = 0;
                     Console.WriteLine(e.ToString());
                 }

           }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
             return r;
        }

   
        #endregion

        #region IRecShop Members


        public List<RecShop> getRecShopSubShop(int cityID, int categoryID, int shopID)
        {
            List<RecShop> c = null;
            try
            {
                c = ds.getRecSubShop(cityID, categoryID, shopID).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
   
            }
            return c;
        }

        #endregion

        #region IRecShop Members

        // Call this function when the shop is votes as support and add 1 support vote to oppose counter
        public int addShopSuport(int shopID)
        {
            int c=0;
            try
            {
                c = ds.updateShopPerformance("support", shopID, 0);
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

       // Call this function when the shop is votes as oppose and add 1 oppose vote to oppose counter
        public int addShopOppose(int shopID)
        {
            int c = 0;
            try
            {
                c = ds.updateShopPerformance("oppose", shopID, 0);
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        // Call this function when the shop page is open and add 1 view to the view counter
        public int addShopView(int shopID)
        {
            int c = 0;
            try
            {
                c = ds.updateShopPerformance("view", shopID, 0);
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        // Call this function to get the top N ranking shop and the shop is based on the latest 7 days view counter
        public List<RecShop> getShopRanking(int top)
        {
            List<RecShop> c=null;

            try
            {
                c = ds.getShopViewRanking("Top", 0, top).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        #endregion

        #region IRecShop Members

        // Call this function to get the view, support and oppose counter value in the past week
        public List<shopPerformanceResult> getShopLatestWeekPerformance(int shopID)
        {
            List<shopPerformanceResult> c = null;

            try
            {
                c = ds.getShopPerformance("LatestWeek",shopID, 0).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        // Call this function to get the view, support and oppose counter value in the past month
        public List<shopPerformanceResult> getShopLatestMonthPerformance(int shopID)
        {
            List<shopPerformanceResult> c = null;

            try
            {
                c = ds.getShopPerformance("LatestMonth", shopID, 0).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        // Call this function to get the view, support and oppose counter value in total
        public List<shopPerformanceResult> getShopLatestTotalPerformance(int shopID)
        {
            List<shopPerformanceResult> c = null;

            try
            {
                c = ds.getShopPerformance("Latest", shopID, 0).ToList();
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());

            }
            return c;
        }

        #endregion
    }
}
